import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '@/views/Main.vue'

Vue.use(VueRouter)

/**
 * Note: 路由配置项
 *
 * hidden: true                   // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true               // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect           // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'             // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * meta : {
    roles: ['admin','editor']    // 设置该路由进入的权限，支持多个权限叠加
    title: 'title'               // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'             // 设置该路由的图标，对应路径src/icons/svg
    breadcrumb: false            // 如果设置为false，则不会在breadcrumb面包屑中显示
    affix: false                 // 如果设置为true，则此页面标签不可关闭
  }
 */

export const routes = [
  {
    path: '/redirect',
    component: Main,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect')
      }
    ]
  },

  {
    path: '/',
    component: Main,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/MainPage/Index'),
        name: 'Index',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    name: "user",
    path: "/user",
    redirect: '/user/usercount',
    hidden: true,
    component: Main,
    meta: {
      title: "用户",
      icon: "manage",
      noCache: false,
      link: null
    },
    children: [
      {
        path: 'usercount',
        component: () => import('@/views/MainPage/User/UserCount'),
        name: 'usercount',
        meta: { title: '用户统计', icon: 'dashboard', affix: false }
      },
      {
        path: 'usermanage',
        component: () => import('@/views/MainPage/User/UserManage'),
        name: 'usermanage',
        meta: { title: '用户管理', icon: 'dashboard', affix: false }
      },
      {
        path: 'usersetting',
        component: () => import('@/views/MainPage/User/UserSetting'),
        name: 'usersetting',
        meta: { title: '用户配置', icon: 'dashboard', affix: false }
      },
    ]
  },
  {
    name: "goods",
    path: "/goods",
    redirect: '/goods/goodscount',
    hidden: true,
    component: Main,
    meta: {
      title: "商品",
      icon: "manage",
      noCache: false,
      link: null,
      affix: false
    },
    children: [
      {
        path: 'goodscount',
        component: () => import('@/views/MainPage/Goods/GoodsCount'),
        name: 'goodscount',
        meta: { title: '商品统计', icon: 'dashboard', affix: false }
      },
      {
        path: 'goodsmanage',
        component: () => import('@/views/MainPage/Goods/GoodsManage'),
        name: 'goodsmanage',
        meta: { title: '商品管理', icon: 'dashboard', affix: false }
      },
      {
        path: 'goodstype',
        component: () => import('@/views/MainPage/Goods/GoodsType'),
        name: 'goodstype',
        meta: { title: '商品分类', icon: 'dashboard', affix: false }
      },
    ]
  },
  { path: '*', redirect: '/' }
]

// 防止连续点击多次路由报错
let routerPush = VueRouter.prototype.push;
let routerReplace = VueRouter.prototype.replace;
// push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push(location) {
  return routerReplace.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  routes
})

export default router
